package com.wawayaya.bookreader.jni;

import android.util.Log;

import java.io.File;

/**
 * 笔画 so lib加载
 */
public class ChnStrokeFiller
{
	private static final String s_strName = "ckl_rd_soft_StrokeFiller";
	private static int s_nLoad = -1;
	private int m_hFiller = 0;
	
	public static boolean initialize(String strPath) {
		if (s_nLoad >= 0)
			return (s_nLoad != 0);
		try {
			boolean bExist = false;
			String strFile = "";
			if (strPath != null && strPath.length() > 0)
			{
				strFile = strPath + "/lib" + s_strName + ".so";
				File f = new File(strFile);
				bExist = f.exists();
			}
			if (bExist)
				System.load(strFile);
			else
				System.loadLibrary(s_strName);
			s_nLoad = 1;
		} catch(UnsatisfiedLinkError e) {
			s_nLoad = 0;
			Log.d("pinyin", e.getMessage());
		}
		return (s_nLoad != 0);
    }
	
	public ChnStrokeFiller() {
		m_hFiller = 0;
	}
	
	public boolean enableFill() {
		if (m_hFiller != 0)
			return true;
		m_hFiller = createFiller();
		return (m_hFiller != 0);
	}
	
	public boolean setSize(int x, int y) {
		if (m_hFiller == 0)
			return false;
		int n = x + (y << 16);
		return setMaxSize(m_hFiller, n) > 0;
	}
	
	public boolean setStrokeData(short[] nContour, short[] nMidLine) {
		if (m_hFiller == 0)
			return false;
		return setData(m_hFiller, nContour, nMidLine) > 0;
	}
	
	public boolean setCacheData(short[] nGuide) {
		if (m_hFiller == 0)
			return false;
		return setGuide(m_hFiller, nGuide) > 0;
	}
	
	public short[] getPoints(int nMaxCount) {
		if (m_hFiller == 0)
			return null;
		return getNextPoints(m_hFiller, nMaxCount);
	}
	
	public void close() {
		if (m_hFiller != 0) {
			destroyFiller(m_hFiller);
			m_hFiller = 0;
		}
	}
	
	private static native int createFiller();
	private static native int setMaxSize(int hFiller, int nValue);
	private static native int setData(int hFiller, short[] nContour, short[] nMidLine);
	private static native int setGuide(int hFiller, short[] nGuide);
	private static native short[] getNextPoints(int hFiller, int nMaxCount);
	private static native int destroyFiller(int hFiller);
	public static native String getVersion();
};
